import axios from 'axios'
import qs from 'qs'
import { message } from 'antd'
import 'antd/dist/antd.css';
import { remove } from './localstorage';

// axios.defaults.baseURL = 'http://draft.com/index.php/'
// axios.defaults.baseURL = 'https://zhiluedu.com.cn/'
axios.defaults.baseURL = 'https://edu.unt56.com/'
// axios.defaults.baseURL = 'http://educations.njql.top/'
// axios.defaults.baseURL = 'http://rx.gszhyun.com/'
// axios.defaults.baseURL = 'http://education.njql.top/'
axios.defaults.timeout = 100000;
// axios.defaults.withCredentials = true;

// let path = 'https://zhiluedu.com.cn/'
let path = 'https://edu.unt56.com/'
// let path = 'http://educations.njql.top/'
// let path = 'http://rx.gszhyun.com/'
// let path = 'http://draft.com/index.php/'

axios.interceptors.response.use(
    (res)=> {
        //console.log(res);
        return res;
    },
    (err)=> {
      // 对响应错误做点什么

      // console.log(err.response.status)
      // console.log(err.response)
      if(err.response.status === 401){
        remove('token');
        message.info('请重新登录！');
        window.location.hash = '/'
      }
      
      return Promise.reject(err);
    }
  );


 let get = function(url,params,loading){
     return new Promise((resolve,reject)=>{
         axios
         .get(url,{params:params})
         .then(res=>{
             resolve(res);
         })
         .catch(err =>{
             reject(err)
         })
     })
 }

 let post = function(url,data){
     return new Promise((resolve,reject)=>{
         axios
         .post(url,qs.stringify(data))
         .then(res =>{
            //  console.log(res);
             resolve(res)
         })
         .catch(err =>{
             reject(err)
         })
     })
 }

 let formData = function(url,data){
  return new Promise((resolve,reject)=>{
      axios
      .post(url,data,{
          headers:{
              'Content-Type': 'multipart/form-data'
          }
      })
      .then(res =>{
          resolve(res)
      })
      .catch(err =>{
          reject(err)
      })
  })
}

const http = { get, post , formData , path }

export default http;